﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

using PowerCreator.LMS.Download.Entity;
using PowerCreator.LMS.Web.Download;
using PowerCreator.LMS.Common.Script;
public partial class sysadmin_DownManager_ManagerDownload : PowerCreator.LMS.Web.Base.WebBasePage
{
    Download objDownload = new Download();
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitPage();
            string search = GetSearch();
            BinderPager(search);
            BinderPG(search, 1);
        }
    }

    protected override void InitPage()
    {
        lbl_InfoTitle_ManagerDownload.Text = GetLanageValueByKey("lab_ManagerDownload_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_ManagerDownload.Text = "新下载资源列表";
        btnAddNew.Text = GetLanageValueByKey("btn_AddNew", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnSearch.Text = GetLanageValueByKey("btn_Search", PowerCreator.LMS.Language.LanagetItemType.Page);

        chkAll.Text = GetLanageValueByKey("chkbox_ManagerQuestion_chkboxAll", PowerCreator.LMS.Language.LanagetItemType.Page);
        chkCancel.Text = GetLanageValueByKey("chkbox_ManagerQuestion_chkboxCancel", PowerCreator.LMS.Language.LanagetItemType.Page);

        btn_DelCheck.Text = GetLanageValueByKey("btnDel", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnAddNew.Text = GetLanageValueByKey("btn_AddNew", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_IsCheck.Text = GetLanageValueByKey("btnAuditing", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_DelCheck.Attributes.Add("onClick", "javascript:return confirm('确定删除此记录？');");
        base.InitPage();

        //btnAddNew.Visible = false;
        //if (IsLicensed(PowerCreator.LMS.Action.Core.ActionInfo.AddJob))
        //    btnAddNew.Visible = true;

    }
    private string GetSearch()
    {
        string search = "";
        if (!string.IsNullOrEmpty(txt_DownLoadTitle.Text))
            search = string.Format("DownName like '%{0}%'", txt_DownLoadTitle.Text.Trim());
        return search;
    }

    protected void BinderPager(string search)
    {
        AspNetPager1.RecordCount = objDownload.Count(search);
        AspNetPager1.PageSize = LMSPageSize;
    }
    private void BinderPG(string search,int pageIndex)
    {
        DataTable dt = objDownload.Query(search, LMSPageSize, pageIndex);
        rptDownload.DataSource = dt;
        rptDownload.DataBind();

        PreAspNetPager(AspNetPager1);
    }  

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderPager(search);
        BinderPG(search,1);
    }
    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        string url = "AddDownload.aspx";
        Script.RedirectURL(url);
    }
  
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderPG(search, AspNetPager1.CurrentPageIndex);
    }
    protected void rptDownload_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        Label lbl_msg = (Label)e.Item.FindControl("lbl_msg");

        if (e.CommandName.ToLower() == "leditor")
        {
            //if (IsLicensed("DownloadUpdate"))
            //{
                string url = "AddDownload.aspx?DownloadID=" + e.CommandArgument.ToString();
                Script.RedirectURL(url);
            //}
            //else
            //{
            //    lbl_msg.Text = GetLanageValueByKey("msg_ManagerNew_checkmsg", PowerCreator.LMS.Language.LanagetItemType.Page);
            //}
        }
        else if (e.CommandName.ToLower() == "ldel")
        {
            //if (IsLicensed("DownloadDel"))
            //{
                int downid = int.Parse(e.CommandArgument.ToString());
                VOTDownload vo = new VOTDownload();
                vo = objDownload.Load(downid);
                if (objDownload.Del(downid))
                {
                    string path = "../../" + vo.DownUrl;
                    string url = Server.MapPath(path);
                    if (System.IO.File.Exists(Server.MapPath(path)))
                    {
                        System.IO.File.Delete(Server.MapPath("../../" + vo.DownUrl));
                    }
                    string search = GetSearch();
                    BinderPager(search);
                    BinderPG(search, AspNetPager1.CurrentPageIndex);
                //}
                //else
                //{
                //    lbl_msg.Text = GetLanageValueByKey("Msg_ManagerQuestion_WorkNo", PowerCreator.LMS.Language.LanagetItemType.Page);
                //}
            }
            else
            {
                lbl_msg.Text = GetLanageValueByKey("msg_ManagerNew_checkmsg", PowerCreator.LMS.Language.LanagetItemType.Page);
            }
        }
        else if (e.CommandName.ToLower() == "ischeck")
        {
            VOTDownload vo = new VOTDownload();
            vo = objDownload.Load(int.Parse(e.CommandArgument.ToString()));
            if (vo.DownState == true)
                vo.DownState = false;
            else
                vo.DownState = true;
            objDownload.Update(vo);
            string search = GetSearch();
            BinderPager(search);
            BinderPG(search, AspNetPager1.CurrentPageIndex);
        }

    }
    protected void rptDownload_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
        {
            LinkButton lbtn = (LinkButton)e.Item.FindControl("lnkbtn_del");
            lbtn.Attributes.Add("onClick", "javascript:return confirm('确定删除此记录？');");
        } 
    }
    protected void chkAll_CheckedChanged(object sender, EventArgs e)
    {
        chkCancel.Checked = false;
        //全选 / 取消
        for (int i = 0; i < rptDownload.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rptDownload.Items[i].FindControl("Chbox_Check");
            if (chkAll.Checked)
                cbox.Checked = true;
            else
                cbox.Checked = false;
        }
    }
    protected void chkCancel_CheckedChanged(object sender, EventArgs e)
    {
        chkAll.Checked = false;
        //反选
        for (int i = 0; i < rptDownload.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rptDownload.Items[i].FindControl("Chbox_Check");
            if (cbox.Checked)
                cbox.Checked = false;
            else
                cbox.Checked = true;
        }
    }
    protected void btn_DelCheck_Click(object sender, EventArgs e)
    {
        try
        {
            for (int i = 0; i < rptDownload.Items.Count; i++)
            {
                CheckBox cbox = (CheckBox)rptDownload.Items[i].FindControl("Chbox_Check");
                Label lab_DownID = (Label)rptDownload.Items[i].FindControl("lab_DownID");
                if (cbox.Checked)
                {
                    //if (IsLicensed("DownloadDel"))
                    //{
                        int downid = int.Parse(lab_DownID.Text);
                        VOTDownload vo = new VOTDownload();
                        vo = objDownload.Load(downid);
                        if (objDownload.Del(downid))
                        {
                            string path = "../../" + vo.DownUrl;
                            string url = Server.MapPath(path);
                            if (System.IO.File.Exists(Server.MapPath(path)))
                            {
                                System.IO.File.Delete(Server.MapPath("../../" + vo.DownUrl));
                            }
                            
                        }
                        else
                        {
                            Script.Alert(this, "msg",  GetLanageValueByKey("Msg_ManagerQuestion_WorkNo", PowerCreator.LMS.Language.LanagetItemType.Page));
                        }
                    //}
                    //else
                    //{
                    //    Script.Alert(this, "msg", GetLanageValueByKey("msg_ManagerNew_checkmsg", PowerCreator.LMS.Language.LanagetItemType.Page));
                    //}
                }
            }
            string search = GetSearch();
            BinderPager(search);
            BinderPG(search, AspNetPager1.CurrentPageIndex);
            Script.Alert(this, "msg", GetLanageValueByKey("Msg_ManagerQuestion_WorkOK", PowerCreator.LMS.Language.LanagetItemType.Page));
        }
        catch
        {
            Script.Alert(this, "msg", GetLanageValueByKey("Msg_ManagerQuestion_WorkNo", PowerCreator.LMS.Language.LanagetItemType.Page));
        }
    }
    protected void btn_IsCheck_Click(object sender, EventArgs e)
    {
        try
        {
            for (int i = 0; i < rptDownload.Items.Count; i++)
            {
                CheckBox cbox = (CheckBox)rptDownload.Items[i].FindControl("Chbox_Check");
                Label lab_DownID = (Label)rptDownload.Items[i].FindControl("lab_DownID");
                if (cbox.Checked)
                {
                    VOTDownload vo = new VOTDownload();
                    vo = objDownload.Load(int.Parse(lab_DownID.Text));
                    if (vo.DownState == true)
                        vo.DownState = false;
                    else
                        vo.DownState = true;
                    objDownload.Update(vo);
                    
                }
            }
            string search = GetSearch();
            BinderPager(search);
            BinderPG(search, AspNetPager1.CurrentPageIndex);
            Script.Alert(this, "msg", GetLanageValueByKey("Msg_ManagerQuestion_WorkOK", PowerCreator.LMS.Language.LanagetItemType.Page));
        }
        catch
        {
            Script.Alert(this, "msg",  GetLanageValueByKey("Msg_ManagerQuestion_WorkNo", PowerCreator.LMS.Language.LanagetItemType.Page));
        }
       
    }
}
